package by.bucha.testproject.content;

import by.bucha.testproject.content.contract.TaskContract;
import by.bucha.testproject.content.database.DatabaseHelper;
import by.bucha.testproject.content.database.tables.WorkerTable;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

public class ApplicationProvider extends ContentProvider {

	private static final UriMatcher sUriMatcher;

	/** The constant for UriMatcher */
	private static final int URI_WORKER = 1;

	/** The constant for UriMatcher */
	private static final int URI_WORKER_ID = 2;

	public static final String EXTRA_DATA = "data";

	public static final String EXTRA_MODEL_TYPE = "model type";

	public static DatabaseHelper mDatabaseHelper;

	@Override
	public boolean onCreate() {
		mDatabaseHelper = new DatabaseHelper(getContext(), null);
		return true;
	}

	static {
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

		sUriMatcher.addURI(TaskContract.AUTHORITY,
				TaskContract.WorkerColumns.PUTH, URI_WORKER);

		sUriMatcher.addURI(TaskContract.AUTHORITY,
				TaskContract.WorkerColumns.PUTH + "/*", URI_WORKER_ID);

	}

	@Override
	public int delete(final Uri uri, final String selection,
			final String[] selectionArgs) {

		int count;

		if (sUriMatcher.match(uri) == URI_WORKER_ID) {

			final String mySelection = selection + "="
					+ uri.getLastPathSegment();
			count = mDatabaseHelper.deleteTasks(mySelection, selectionArgs,
					WorkerTable.WORKER_TABLE);

		} else if (sUriMatcher.match(uri) == URI_WORKER) {

			count = mDatabaseHelper.deleteTasks(selection, selectionArgs,
					WorkerTable.WORKER_TABLE);

		} else {

			throw new IllegalArgumentException("Unknown URI " + uri);

		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public String getType(final Uri uri) {

		String type = null;

		if (sUriMatcher.match(uri) == URI_WORKER) {

			type = TaskContract.WorkerColumns.CONTENT_TYPE;

		} else {

			throw new IllegalArgumentException("Unknown URI " + uri);

		}
		return type;
	}

	@Override
	public Uri insert(final Uri uri, final ContentValues values) {

		String jsonData = values.getAsString(EXTRA_DATA);

		String dataType = values.getAsString(EXTRA_MODEL_TYPE);

		Uri taskUri;

		if (sUriMatcher.match(uri) == URI_WORKER) {

			ContentValues valuesToInsert = WorkerTable.getContentValues(
					jsonData, dataType);

			long id = mDatabaseHelper.addTasks(valuesToInsert,
					WorkerTable.WORKER_TABLE);
			taskUri = ContentUris.withAppendedId(
					TaskContract.WorkerColumns.CONTENT_URI, id);

		} else {

			throw new IllegalArgumentException("Unknown URI " + uri);

		}

		getContext().getContentResolver().notifyChange(taskUri, null);
		return taskUri;
	}

	@Override
	public Cursor query(final Uri uri, final String[] columns,
			final String selection, final String[] selectionArgs,
			final String sortOrder) {

		Cursor data = null;
		if (sUriMatcher.match(uri) == URI_WORKER_ID) {

			final String mySelection = selection + "="
					+ uri.getLastPathSegment();
			data = mDatabaseHelper.getTasks(columns, mySelection,
					selectionArgs, null, null, sortOrder,
					WorkerTable.WORKER_TABLE);

		} else if (sUriMatcher.match(uri) == URI_WORKER) {

			data = mDatabaseHelper.getTasks(columns, selection, selectionArgs,
					null, null, sortOrder, WorkerTable.WORKER_TABLE);

		} else {

			throw new IllegalArgumentException("Unknown URI " + uri);

		}

		data.setNotificationUri(getContext().getContentResolver(), uri);
		return data;
	}

	@Override
	public int update(final Uri uri, final ContentValues values,
			final String selection, final String[] selectionArgs) {

		int count;

		String jsonData = values.getAsString(EXTRA_DATA);

		String dataType = values.getAsString(EXTRA_MODEL_TYPE);

		if (sUriMatcher.match(uri) == URI_WORKER_ID) {

			final String mySelection = selection + "="
					+ uri.getLastPathSegment();

			ContentValues valuesToUpdate = WorkerTable.getContentValues(
					jsonData, dataType);

			count = mDatabaseHelper.updateTasks(valuesToUpdate, mySelection,
					selectionArgs, WorkerTable.WORKER_TABLE);

		} else if (sUriMatcher.match(uri) == URI_WORKER) {

			ContentValues valuesToUpdate = WorkerTable.getContentValues(
					jsonData, dataType);

			count = mDatabaseHelper.updateTasks(valuesToUpdate, selection,
					selectionArgs, WorkerTable.WORKER_TABLE);

		} else {

			throw new IllegalArgumentException("Unknown URI " + uri);

		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}
}
